package com.gj.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.DataOutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

class ServerTest2 {

 public static void main(String[] args) throws IOException {
  //InetAddress addr = InetAddress.getByName("192.168.4.221");
  ServerSocket server = new ServerSocket(8090);
  Socket client = server.accept();
  BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  PrintWriter out = new PrintWriter(client.getOutputStream());
  DataOutputStream back = new DataOutputStream(client.getOutputStream());
  int ch;
 //byte[] ip = {(byte)0xaa,(byte)192,(byte)168,(byte)1,(byte)1};

  back.writeByte((byte)0xaa);
  back.writeByte((byte)0xbb);

  for (int i = 0; i <= 255; i++) {
   System.out.println(i + "");
    back.writeByte(i);
  }

//  back.writeByte(127);
//  back.writeByte((byte)168);
//  back.writeByte((byte)4);
//  back.writeByte((byte)146);
//  back.writeByte((byte)0xaa);
//  back.writeByte((byte)0xcc);
//  back.writeByte((byte)192);
//  back.writeByte((byte)168);
//  back.writeByte((byte)4);
//  back.writeByte((byte)1);
//  back.writeByte((byte)0xaa);
//  back.writeByte((byte)0xdd);
//  back.writeByte((byte)255);
//  back.writeByte((byte)254);
//  back.writeByte((byte)255);
//  back.writeByte((byte)0);
//  back.writeByte((byte)0xaa);
//  back.writeByte((byte)0xee);
//  back.writeByte((byte)192);
//  back.writeByte((byte)168);
//  back.writeByte((byte)4);
//  back.writeByte((byte)221);

  System.out.println("发送数据到客户端");

  back.flush();
  while(true){
   String sen;
   if((ch=in.read()) != -1)
   {
    sen = intTohex(ch);
    System.out.println(String.valueOf(ch));
    if(ch==65533)
    {
     System.out.println("hree");
     //client.close();
     //client = server.accept();
     //in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    }
   }
  }
 }
 public static String intTohex(int n) {
  StringBuffer s = new StringBuffer();
  String a;
  char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
  while (n != 0) {
   s = s.append(b[n % 16]);
   n = n / 16;
  }
  a = s.reverse().toString();
  if ("".equals(a)) {
   a = "00";
  }
  if (a.length() == 1) {
   a = "0" + a;
  }
  return a;
 }

}